<template>
      <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
          <el-form-item label="appid" prop="appid">
            <el-input
              v-model="queryParams.appid"
              placeholder="请输入appid"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="公众号名" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入公众号名称"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
         <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['wx:account:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['wx:account:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['wx:account:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['wx:account:export']"
            >导出</el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="wxAccountList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
<!--          <el-table-column label="ID" align="center" prop="id" />-->
          <el-table-column label="appid" align="center" prop="appid" :show-overflow-tooltip="true" />
          <el-table-column label="公众号名称" align="center" prop="name" :show-overflow-tooltip="true" />
          <el-table-column label="类型" align="center" prop="type" :formatter="accountTypeFormat"/>
          <el-table-column label="是否认证" align="center" prop="verified" :show-overflow-tooltip="true" >
            <span slot-scope="scope">{{scope.row.verified==='1'?"是":"否"}}</span>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                size="mini"
                circle type="info"
                icon="el-icon-view"
                @click="accessInfo(scope.row)"
                v-hasPermi="['wx:account:query']"
              />
              <el-button
                size="mini"
                circle type="success"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['wx:account:edit']"
              />
              <el-button
                size="mini"
                circle type="danger"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['wx:account:remove']"
              />
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 添加或修改参数配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px">
          <el-form :model="form" :rules="rules" ref="form" @keyup.enter.native="dataFormSubmit()" label-width="100px">
            <el-form-item label="公众号名称" prop="name">
              <el-input v-model="form.name" placeholder="公众号名称"></el-input>
            </el-form-item>
            <div class="padding text-gray">
              测试号可选择服务号，不同类型账号、是否认证可使用功能权限不同，
              <el-link type="primary" target="_blank" href="https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Explanation_of_interface_privileges.html">参考文档</el-link>
            </div>
            <el-row>
              <el-col :span="12">
                <el-form-item label="公众号类型" prop="type">
                  <el-select v-model="form.type" placeholder="公众号类型">
                    <el-option v-for="item in ACCOUNT_TYPES" :key="item.value" :label="item.label" :value="item.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否认证" prop="verified">
                  <el-switch v-model="form.verified" placeholder="是否认证"
                             active-value="1"
                             inactive-value="0">

                  </el-switch>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="appid" prop="appid">
              <el-input v-model="form.appid" placeholder="appid"></el-input>
            </el-form-item>
            <el-form-item label="appsecret" prop="secret">
              <el-input v-model="form.secret" placeholder="appsecret"></el-input>
            </el-form-item>
            <el-form-item label="token" prop="token">
              <el-input v-model="form.token" placeholder="token"></el-input>
            </el-form-item>
            <el-form-item label="aesKey" prop="aesKey">
              <el-input v-model="form.aesKey" placeholder="aesKey，可为空"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
        <!-- 弹窗, 查看 -->
        <account-access v-if="accountAccessVisible" ref="accountAccessDialog"></account-access>
    </div>
</template>

<script>
import { listWxAccount, getWxAccount, delWxAccount, addWxAccount, updateWxAccount, exportWxAccount } from "@/api/wx/account";

import AccountAccess from './wx-account-access-info'
import { mapState } from 'vuex'
export default {
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 参数表格数据
        wxAccountList: [],
        //开发接入信息弹窗
        accountAccessVisible:false,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 类型数据字典
        typeOptions: [],
        // 日期范围
        dateRange: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          appid:undefined,
          name:undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          appid: [
            { required: true, message: "appid不能为空", trigger: "blur" }
          ],
          secret: [
            { required: true, message: "appsecret不能为空", trigger: "blur" }
          ],
          name: [
            { required: true, message: "公众号名称不能为空", trigger: "blur" }
          ],
          token: [
            { required: true, message: "token不能为空", trigger: "blur" }
          ]
        }

        }
    },
    components: {AccountAccess  },
    computed: mapState({
        ACCOUNT_TYPES: state=>state.wxAccount.ACCOUNT_TYPES
    }),
    created() {
        this.getList()
    },
    methods: {
        // 获取数据列表
      getList() {
          this.loading = true;
          listWxAccount(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
              this.wxAccountList = response.data.list;
              this.total = response.data.total;
              this.$store.commit('wxAccount/updateAccountList',response.data.list)
              this.loading = false;
            }
          );
        },
      // 参数系统内置字典翻译
      typeFormat(row, column) {
        return this.selectDictLabel(this.typeOptions, row.paramType);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加参数";
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getWxAccount(id).then(response => {
          this.form = response.data
          this.open = true;
          this.title = "修改参数";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id !== undefined) {
              updateWxAccount(this.form.id,this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.message);
                }
              });
            } else {
              addWxAccount(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.message);
                }
              });
            }
          }
        });
      },
      accessInfo(item){
        this.accountAccessVisible = true
        this.$nextTick(() => {
          this.$refs.accountAccessDialog.init(item)
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除参数编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delWxAccount(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有参数数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportWxAccount(queryParams);
        }).then(response => {
          this.fileDownload(response.data, "公众号账号.xlsx");
        }).catch(function() {});
      },
      accountTypeFormat(row, column) {
        const actions = [];
        Object.keys(this.ACCOUNT_TYPES).map((key) => {
          if (this.ACCOUNT_TYPES[key].value === row.type) {
            actions.push(this.ACCOUNT_TYPES[key].label);
            return false;
          }
        })
        return actions.join('');
      }
    }
}
</script>
